#! /usr/bin/env node

import { compressDir } from "../tinypng/TinyHtml";
import { compressDirJs } from "../utils/JsCompress";

import inquirer = require("inquirer");
import chalk = require("chalk");

function logFunc(msg) {
    console.log(msg);
}

function askInput(msg: string, key: string) {
    let questions = [
        {
            type: "input",
            name: key,
            message: msg,
        },
    ];
    return inquirer.prompt(questions);
};

function askConfirm(msg: string, key: string) {
    let questions = [
        {
            type: 'confirm',
            name: key,
            message: msg
        }
    ];
    return inquirer.prompt(questions);
};

function logHelp() {
    console.log('\n');
    console.log("Usage: compress [options]\n");
    console.log("Options:");
    console.log("   -j, --js            js文件压缩");
    console.log("   -p, --picture       png/jpg文件压缩");
    console.log('\n');
}

async function run(argv) {

    if (argv[0]) {
        let argv1 = argv[0].toLowerCase();
        if (argv1 === '-j' || argv1 === '--js') {
            let path = await askInput('请输入文件路径', 'path');
            let toPath = await askInput('请输入目标路径', 'toPath');
            let es6Toes5 = await askConfirm('是否ES6转ES5', 'es6Toes5');
            console.log(chalk.yellow("===========================START============================"));
            compressDirJs(path['path'], toPath['toPath'], function () {
                console.log(chalk.yellow("===========================END============================"));
            }, logFunc, es6Toes5['es6Toes5']);
        }
        else if (argv1 === '-p' || argv1 === '--png') {
            let path = await askInput('请输入文件路径', 'path');
            console.log(chalk.yellow("===========================START============================"));
            compressDir(path['path'], function () {
                console.log(chalk.yellow("===========================END============================"));
            }, logFunc);
        }
        else {
            logHelp();
        }
    }
    else {
        logHelp();
    }
}

run(process.argv.slice(2));